package com.chenglin.picturepicker.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.chenglin.picturepicker.R
import com.chenglin.picturepicker.bean.MediaBean
import com.chenglin.picturepicker.databinding.AdapterItemBinding

class ImageAdapter(var context: Context,var list: List<MediaBean>,var callBack : ImageAdapterCallBack) :RecyclerView.Adapter<ImageAdapter.MyHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val inflate = AdapterItemBinding.inflate(LayoutInflater.from(context), parent, false)
        return MyHolder(inflate)
    }


    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        if (list[position].isSelect){
            holder.binding.ivSelectTag.setBackgroundResource(R.drawable.image_selecor_selected)
        }else{
            holder.binding.ivSelectTag.setBackgroundResource(R.drawable.image_selector_unselected)
        }
        Glide.with(context).load(list[position].path).override(200,200).skipMemoryCache(true).into( holder.binding.ivSelect)
        holder.binding.ivSelect.setOnClickListener {
            callBack.onClickItem(position,list[position])
        }

        holder.binding.ivSelectTag.setOnClickListener {
            list[position].isSelect = !list[position].isSelect
            callBack.onClickTag(position,list[position])
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    class MyHolder(var binding: AdapterItemBinding) : RecyclerView.ViewHolder(binding.root) {

    }

    interface ImageAdapterCallBack{
        fun onClickItem(position: Int,bean: MediaBean)
        fun onClickTag(position: Int,bean: MediaBean)
    }



}

